$(function () {

    get_shop();
    get_shoparea();
    get_gspro();

    //获取凑单品店铺数据
    function get_shop() {
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getgsshop",
            dataType: "json",
            success: function (res) {
                console.log(res);
                $(".filter-shop span").text(res.result[0].shopName);
                $(".filter-shop span").attr("data-shopId", res.result[0].shopId);

                var str = "";
                $.each(res.result, function (index, value) {
                    str += "<li data-shopId=" + res.result[index].shopId + ">" + res.result[index].shopName + "</li>"

                });
                $("#gsShop").html(str);               
            }
        });
    }

    //获取凑单品区域数据
    function get_shoparea() {
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getgsshoparea",
            dataType: "json",
            success: function (res) {
                console.log(res);
                $(".filter-gsArea span").text(res.result[0].areaName.substring(0, 2));
                $(".filter-gsArea span").attr("data-areaId", res.result[0].areaId);

                var str = "";
                $.each(res.result, function (index, value) {
                    str += "<li data-areaId=" + res.result[index].areaId + ">" + res.result[index].areaName + "</li>"

                });
                $("#gsArea").html(str);                
            }
        });
    }


    //区域模块展开和隐藏
    $(".filter-gsArea").click(function () {
        $("#gsArea").toggle();
    });

    //店铺模块展开和隐藏
    $(".filter-shop").click(function () {
        $("#gsShop").toggle();
    });

    //事件委派点击店铺替换显示店铺
    $("#gsShop").on("click","li",function(){
        $(".filter-shop span").text($(this).text());
        $(".filter-shop span").attr("data-shopId", $(this).attr("data-shopId"));

        get_gspro();
    });

    //事件委派点击区域替换显示区域
    $("#gsArea").on("click","li",function(){
        $(".filter-gsArea span").text($(this).text().substring(0,2));
        $(".filter-gsArea span").attr("data-areaId", $(this).attr("data-areaId"));

        get_gspro();
    });


    
    //获取凑单品商品列表
    function get_gspro() { 
        //获取店铺ID和区域ID
        var shopid  = $(".filter-shop span").attr("data-shopId");
        var areaid  = $(".filter-gsArea span").attr("data-areaId");
        
        //判断为undefined时，默认数值为0
        if (shopid == undefined) {
            shopid = 0;
        }
        if (areaid == undefined) {
            areaid = 0;
        }

        console.log(shopid);
        console.log(areaid);


        //空数据没办法操作了
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getgsproduct",
            data:{
                shopId:0,
                areaId:0
            },
            dataType: "json",
            success: function (res) {
                console.log(res);
            }
        });
     }




})